
var redis = require('redis');
var $config = require('../../config');
var redisclient = redis.createClient($config.game.redis);

module.exports = function(io) {

    io.of("room").on('connection', (socket) =>{


        socket.on('chat', function (data) {

            io.of("room").in(data.roomId).emit('chatMessage', data.message);
            let rooms = Object.keys(socket.rooms);
            console.log("rooms",rooms)
        });

        socket.on('add', function (data) {
            console.log("'add room",data)
            redisclient.get(data.roomId,function (result) {

                console.log("roomId:",data.roomId,",value:",result)
                if(result!==null){
                    socket.join(data.roomId,function () {
                        console.log("join",data.roomId,"success",io.sockets.manager.rooms)
                        io.in(data.roomId).emit('chatMessage', {data:"玩家XX加入了房间"});


                    })
                }else{
                    socket.emit('chatMessage',"房间不存在，加入失败")
                }

            })



        });

        socket.on('leave', function (data) {
            socket.leave(data.roomId,function () {
                console.log("join",data.roomId,"success")

            })
        });



    });
}



